%原始生成脚本，暂时不更新
[buses, branches, substations, init_sw] = yantian_vdata('.\data');

system_prompt = readlines("task description-maxload.txt");
system_prompt = strjoin(system_prompt, '\n');

user_prompt_prototype=readlines(".\data\yantian_grid.txt");
user_prompt_prototype = strjoin(user_prompt_prototype, '\n');

msg(1).role = 'system';
msg(1).content = system_prompt;
msg(2).role = 'user';
msg(2).content = "";
msg(3).role = 'assistant';
msg(3).content = "";

load .\data\solution.mat

Nfaults = length(sol);
[fault_cmd, solution_cmd] = sample_fault_tiny(fault_br_name, sol, substations, buses, branches, G, initial_grid);

fileID = fopen('data.jsonl', 'w');
pretty_print_ind=5;
for k = 1:Nfaults
    msg(2).content = user_prompt_prototype+"\n"+fault_cmd(k);
    msg(3).content = solution_cmd(k);
    jsonStr = jsonencode(struct('messages', msg));
    fprintf(fileID, '%s\n', jsonStr);

    %选一个规范输出
    if k==pretty_print_ind
        disp(system_prompt);
        disp(msg(2).content);
        disp(solution_cmd(k));
    end
end

fclose(fileID);


function [fault_cmd, solution_cmd] = sample_fault_tiny(fault_br_name, sol_cell, subs, buses,branches, G, initial_grid)
    Nfaults = length(sol_cell);

    fault_br_name = table(fault_br_name,'VariableNames',{'name'});
    fault_buses=innerjoin(fault_br_name,branches,'Keys','name','RightVariables',{'bus1','bus2'});
    fault_cmd =  compose("The fault occurs at line between bus %s and bus %s.",fault_buses.bus1, fault_buses.bus2);

    solution_cmd = strings(Nfaults, 1);
    src_conn = subs(:,{'bus','busid'});
    loadidx=buses.Pd>0 | buses.Qd>0;
    bus_supply = buses(loadidx,{'bus','busid'});
    for k = 1:Nfaults
        % 查询动作的开关
        if isempty(sol_cell{k}) || sol_cell{k}.success ~= 1
            %无可行解的没有动作
            solution.success = false;
            solution.action = [];
            solution.supplied_loads = [];
            solution_cmd(k) = jsonencode(solution, "PrettyPrint", false);
            continue
        end

        solution.success = true;
        idx = xor(sol_cell{k}.branch_status.status, initial_grid);
        action_sw = table(G.Edges.name(idx),'variableNames',{'name'});
        action_sw=innerjoin(action_sw,branches,'Keys','name','RightVariables',{'bus1','bus2'});
        solution.action = compose("(%s,%s,%d)", action_sw.bus1,action_sw.bus2, int32(sol_cell{k}.branch_status.status(idx)));

        % 查询每条母线从哪一条馈线得电
        idx = find(~sol_cell{k}.branch_status.status);
        H = rmedge(G, idx);
        %连通图算法
        bins = conncomp(H);
        bins = reshape(bins, [], 1);
        %连接表
        %暂时不检查双电源供电的情况
        src_conn.conn=bins(src_conn.busid);
        solution.supplied_loads=[];
        bus_supply.conn = bins(bus_supply.busid);
        % bus_supply = outerjoin(bus_supply, src_conn, 'Keys', 'conn', 'RightVariables', {'supply'});
        % bus_supply(:, 'supply') = fillmissing(bus_supply(:, 'supply'), 'constant', "offline");
        offline_idx=true(height(bus_supply),1);
        for ss = 1:height(src_conn)
            idxss=(bus_supply.conn==src_conn.conn(ss));
             offline_idx= offline_idx & ~idxss;
            solution.supplied_loads.("F"+src_conn.bus(ss)) = bus_supply.bus(idxss);
        end
        solution.supplied_loads.offline = bus_supply.bus(offline_idx);
        solution_cmd(k) = jsonencode(solution,"PrettyPrint",false);
    end

end